﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities.Dispatcher;
using Common.Utilities;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TableDataExtractorOutput:ResultBase
    {
        #region props
        private string _ResultFilePath;
        /// <summary></summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }

        private int _FileProcessed;
        /// <summary></summary>
        public int FileProcessed
        {
            get
            {
                return this._FileProcessed;
            }
            set
            {
                this._FileProcessed = value;
            }
        }

        private int _RecordsCount;
        /// <summary></summary>
        public int RecordsCount
        {
            get
            {
                return this._RecordsCount;
            }
            set
            {
                this._RecordsCount = value;
            }
        }
        #endregion

        #region ctor
        public TableDataExtractorOutput()
            : base(Guid.Empty, Guid.Empty)
        {
            this._ResultFilePath = string.Empty;
            this._FileProcessed = 0;
            this._RecordsCount = 0;
        }

        public TableDataExtractorOutput(Guid componentID, Guid jobTicketID,
            string resultFilePath, int fileCount, int recordCount)
            : base(componentID, jobTicketID)
        {
            this._ResultFilePath = resultFilePath;
            this._FileProcessed = fileCount;
            this._RecordsCount = recordCount;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TableDataExtractorOutput output = new TableDataExtractorOutput();

                output.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");
                output.FileProcessed = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "FileProcessed", "0"));
                output.RecordsCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "RecordCount", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileProcessed", this.FileProcessed.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RecordCount", this.RecordsCount.ToString());

            return xNode;
        }
    }
}
